#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <sys/socket.h>

#include "Protocol.hpp"

namespace client
{
    class CalClient
    {
    public:
        CalClient(const std::string &serverip, const uint16_t &serverport)
            : _sock(-1), _serverip(serverip), _port(serverport)
        {
        }
        void initClient()
        {
            // 1. 创建socket套接字
            _sock = socket(AF_INET, SOCK_STREAM, 0);
            if (_sock < 0)
            {
                std::cerr << "socket create error" << std::endl;
                exit(2);
            }
            // 2. 客户端需要bind, 但是OS自动bind, 不需要显示的bind
            // 3. 不需要listen, 因为我们是一对多的那个多, 为我们服务的只有一台服务器
            // 4. 也不需要accept!
            // 5. 而是要向服务器发起连接!
        }
        void start()
        {
            struct sockaddr_in server;
            memset(&server, 0, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_port = htons(_port);
            server.sin_addr.s_addr = inet_addr(_serverip.c_str());

            if (connect(_sock, (struct sockaddr *)&server, sizeof(server)) != 0)
            {
                std::cerr << "socket connect error" << std::endl;
            }
            else
            {
                std::string line;
                std::string inbuffer;
                while (true)
                {
                    std::cout << "mycal>>> ";
                    std::getline(std::cin, line); // 1+1 没有换行符
                    // 命令行的提取
                    Request req = ParseLine(line);
                    std::string content;
                    req.serialize(&content);
                    std::string send_string = enLength(content);
                    send(_sock, send_string.c_str(), send_string.size(), 0);
                    std::string package, text;

                    if (!recvPackage(_sock, inbuffer, &package))
                        continue;
                    // "content_lin"\r\n"exitcode result"
                    if (!deLength(package, &text))
                        continue;
                    // "exitcode result"
                    Response resp;
                    resp.deserialize(text);
                    std::cout << "exitcode : " << resp.exitcode << std::endl;
                    std::cout << "result: " << resp.result << std::endl;
                }
            }
        }
        Request ParseLine(const std::string &line)
        {
            int status = 0; // 0: 开始 1: 操作符 2: 操作符之后
            int i = 0;
            char op;
            int cnt = line.size();
            std::string left, right;
            while (i < cnt)
            {
                switch (status)
                {
                case 0:
                    if(!isdigit(line[i])) status = 1;
                    else left.push_back(line[i++]);
                    break;
                case 1:
                    op = line[i++];
                    status = 2;
                    break;
                case 2:
                    right.push_back(line[i++]);
                    break;
                }
            }
            return Request(std::stoi(left), std::stoi(right), op);
        }
        ~CalClient()
        {
            if (_sock >= 0)
                close(_sock);
        }

    private:
        int _sock;
        std::string _serverip;
        uint16_t _port;
    };
}